package com.ceiling.service;

import com.ceiling.bean.goods.*;
import com.ceiling.bean.goods.vo.*;
import com.ceiling.dao.BrandMapper;
import com.ceiling.dao.CategoryMapper;
import com.ceiling.dao.GoodsMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    BrandMapper brandMapper;

    @Override

    public GoodsDataVo selectGoodsData(Integer page, Integer limit, String sort, String order, String goodsSn, String name) {

        PageHelper.startPage(page, limit);

        GoodsExample goodsExample = new GoodsExample();

        goodsExample.setOrderByClause(sort + " " + order);
        GoodsExample.Criteria criteria = goodsExample.createCriteria();

        if (goodsSn != null && !"".equals(goodsSn)) {
            criteria.andGoodsSnEqualTo(goodsSn);
        }
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }

        List<Goods> goods = goodsMapper.selectByExample(goodsExample);

        PageInfo<Goods> goodsPageInfo = new PageInfo<>(goods);
        long total = goodsPageInfo.getTotal();

        GoodsDataVo goodsDataVo = new GoodsDataVo();
        goodsDataVo.setTotal(total);
        goodsDataVo.setItems(goods);

        return goodsDataVo;

    }

    @Override
    public CatAndBrandVo catAndBrand() {

        List<CategoryListVo> categoryListVos = categoryMapper.selectCategory(0);

        List<BrandListVo> brandListVos = brandMapper.selectAllBrand();


        CatAndBrandVo catAndBrandVO = new CatAndBrandVo();
        catAndBrandVO.setCategoryList(categoryListVos);
        catAndBrandVO.setBrandList(brandListVos);

        return catAndBrandVO;

    }


}
